package com.myacg.editor.common.unit.html;

import java.util.ArrayList;
import java.util.List;

public class ElementBlock {
	protected  String name;
	protected  AttrItems   attrItems ;
	private  ElementBlock  parent;
	private  List<ElementBlock>  childs;
	public ElementBlock(String name){
		this.name=name;
		attrItems  = new AttrItems();
		childs   = new ArrayList<>();
		parent  = null ; 
	}
	
	public ElementBlock addAttribute(String name ,String value){
		this.attrItems.add(name, value);
		return this;
	}
	public ElementBlock addChild(ElementBlock elementBlock){
		if(elementBlock != null ){
			elementBlock.parent = this;
			childs.add(elementBlock);
		}
		return this;
	} 
	
	/**
	 * 添加普通元素 并填充文本
	 * @param name
	 * @param text
	 * @return
	 */
	public ElementBlock addElementChild(String name , String text){
		if(name != null && name.length()>0){
			this.addChild(new ElementBlock(name).appendText(text));
		}
		return this;
	}
	/**
	 * 添加Input类型的html标签(单标签)
	 * @param typeName
	 * @param value
	 * @return
	 */
	public ElementBlock addInputElementChild(String typeName , String value){
		if(typeName != null && typeName.length()>0){
			this.addChild(new InputElementBlock(typeName,value));
		}
		return this;
	}
	
	/**
	 * 为本节点加入普通的文本内容 ，（无节点标签）
	 * @param text
	 * @return
	 */
	public ElementBlock  appendText(String text){
		if(text!=null && text.length()>0){
			//取最后一个文本节点
			if(childs.size()>0 && TextElementBlock.FINAL_NAME.equals(childs.get(childs.size()-1).name)){
				//在文本节点中追加字符   
				((TextElementBlock)childs.get(childs.size()-1)).append(text);
			}else{
				//没有就新增
				addChild(new TextElementBlock(text));
			}
		}
		return this;
	}
	
	/**
	 * 获取HTML形式的输出
	 * @return
	 */
	public   String getInnerHtml(){
		 StringBuffer  htmlStr = new StringBuffer();
		 loadElementToStringbuffer(this, htmlStr);
		 return htmlStr.toString();
	}
	/**
	 * 将本元素中的内容转换为标签节点递归加入字符串缓存htmlStr中
	 * @param elementBlock  需要转换的节点
	 * @param htmlStr	字符缓存
	 */
	public  void  loadElementToStringbuffer(ElementBlock elementBlock , StringBuffer  htmlStr){
		List<ElementBlock> childs = elementBlock.childs; 
		htmlStr.append(elementBlock.getStartTag()); //首标签
		for (ElementBlock child : childs) {
			htmlStr.append(child.getInnerHtml()); //内容
		} 
		htmlStr.append(elementBlock.getEndTag());//尾标签
	}
	
	/**
	 * 获取首标签
	 * @return
	 */
	public String getStartTag(){
		return  "<"+this.name+(attrItems.print().length()>0 ? " "+attrItems.print()+" " : "")+">";
	} 
	
	/**
	 * 获取尾标签
	 * @return
	 */
	public String getEndTag(){
		return  "</"+this.name+">";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public AttrItems getAttrItems() {
		return attrItems;
	}

	public void setAttrItems(AttrItems attrItems) {
		this.attrItems = attrItems;
	}

	public ElementBlock getParent() {
		return parent;
	}

	public void setParent(ElementBlock parent) {
		this.parent = parent;
	}

	public List<ElementBlock> getChilds() {
		return childs;
	}

	public void setChilds(List<ElementBlock> childs) {
		this.childs = childs;
	}
	
	
}
